package cn.lyjuan.pojoutils.genpojo;

import cn.lyjuan.base.util.StringUtils;
import cn.lyjuan.pojoutils.bean.ColumnBean;
import cn.lyjuan.pojoutils.bean.DBType;
import cn.lyjuan.pojoutils.bean.RunType;
import cn.lyjuan.pojoutils.bean.TableBean;
import cn.lyjuan.pojoutils.factory.GenClassFactory;
import cn.lyjuan.pojoutils.genclass.IGenClass;
import cn.lyjuan.pojoutils.genclass.ITableInfo;
import cn.lyjuan.pojoutils.template.IMapperTemplate;
import cn.lyjuan.pojoutils.template.IMapperXMLTemplate;
import cn.lyjuan.pojoutils.util.ConnUtils;
import cn.lyjuan.pojoutils.util.FileUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ly on 2014/11/23.
 */
public class GenPojo
{
    public static void confirmStart(TableBean tableBean) throws IOException
    {
        System.out.println("Please input Y to continue Gen " + tableBean.getPojoName() + " by " + tableBean.getRunType() + "\n");
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        String str = in.readLine();

        if (StringUtils.isNull(str) || !"y".equals(str.toLowerCase()))
        {
            System.out.println("run is close by user");
            System.exit(0);
        }
    }

    public static void genPojo(TableBean tableBean) throws Exception
    {
        confirmStart(tableBean);// 确认启动

        // 加载数据库驱动
        ConnUtils.loadDBDriver(tableBean.getDriver());

        DBType dbType = ConnUtils.getDBType(tableBean.getDriver());
        // 指定工具
        ITableInfo tableInfo = GenClassFactory.disTableInfo(dbType);

        IGenClass genClass = GenClassFactory.disGenClass(dbType);

        IMapperTemplate mapperTemplate = GenClassFactory.disMapperTemplate(dbType);

        IMapperXMLTemplate mapperXMLTemplate = GenClassFactory.disMapperXMLTemplate(dbType);
        // 解析表和字段信息
        List<ColumnBean> cols = new ArrayList<ColumnBean>();
        tableInfo.parseInfo(tableBean, cols);
        // 生成文件内容字符串
        String classStr = genClass.gen(tableBean, cols);

        String mapperStr = mapperTemplate.genMapperInterface(tableBean, cols);

        String mapperXmlStr = mapperXMLTemplate.genMapperXml(tableBean, cols);

        // 保存文件
        // Pojo
        if (tableBean.getRunType() == RunType.PRINT_POJO
                || tableBean.getRunType() == RunType.PRINT_ALL)
            System.out.println(tableBean.getPojoName() + ".java :\n" + classStr);
        if (tableBean.getRunType() == RunType.SAVE_POJO
                || tableBean.getRunType() == RunType.SAVE_ALL)
            FileUtils.saveClass(tableBean.getPojoPkgName(), tableBean.getPojoName() + ".java", classStr,
                    tableBean.getPojoSrcName());
        // Mapper Interface
        if (tableBean.getRunType() == RunType.PRINT_MAPPER_INTERFACE
                || tableBean.getRunType() == RunType.PRINT_ALL)
            System.out.println(tableBean.getMapperName() + ".java :\n" + mapperStr);
        if (tableBean.getRunType() == RunType.SAVE_MAPPER_INTERFACE
                || tableBean.getRunType() == RunType.SAVE_ALL)
            FileUtils.saveClass(tableBean.getMapperPkgName(), tableBean.getMapperName() + ".java", mapperStr,
                    tableBean.getMapperSrcName());
        // Mapper XML
        if (tableBean.getRunType() == RunType.PRINT_MAPPER_XML
                || tableBean.getRunType() == RunType.PRINT_ALL)
            System.out.println(tableBean.getMapperName() + ".xml :\n" + mapperXmlStr);
        if (tableBean.getRunType() == RunType.SAVE_MAPPER_XML
                || tableBean.getRunType() == RunType.SAVE_ALL)
            FileUtils.saveClass(tableBean.getMapperPkgName(), tableBean.getMapperName() + ".xml", mapperXmlStr,
                    tableBean.getMapperXmlSrcName());
    }
}
